package model

type FoodServer interface {
	Create(invite *FoodList) (err error)
	GetStock(openId string) (stock string, err error)
	SetStock(openId string, stock string) (err error)
	OpenStore(openId string) (err error)
	StoreLevelUp(openId string, level int) (err error)
	TaskDataUp(openId string, taskData string) (err error)
	TaskLevelUp(openId string, taskLevel string) (err error)
	UpCompletes(openId string, completes string) (err error)
	GetRow(openId string) (food FoodList, err error)
	Count(openId string) (count int)
}

type foodListServer struct {
}

func NewFoodServer() FoodServer {
	return &foodListServer{}
}

func (u *foodListServer) Create(invite *FoodList) (err error) {
	sql := "INSERT INTO food_list (open_id, stock, levels, opened, completes, task_level, task_data) VALUES (?, ?, ?, ?, ?, ?, ?)"
	stmt, err := Db.Prepare(sql)
	if err != nil {
		return
	}
	defer stmt.Close()

	res, err := stmt.Exec(invite.OpenId, invite.Stock, invite.Level, invite.OpenId, invite.Completes, invite.TaskLevel, invite.TaskData)
	if err != nil {
		return
	}
	id, err := res.LastInsertId()
	if err != nil {
		return
	}
	invite.Id = int(id)
	return nil
}

func (u *foodListServer) GetRow(openId string) (food FoodList, err error) {
	err = Db.QueryRow("SELECT stock, levels, opened, completes, task_level, task_data FROM food_list WHERE open_id = ?", openId).Scan(&food.Stock, &food.Level, &food.Opened, &food.Completes, &food.TaskLevel, &food.TaskData)
	return
}

func (u *foodListServer) Count(openId string) (count int) {
	_ = Db.QueryRow("SELECT count(id) FROM `food_list` WHERE open_id = ?", openId).Scan(&count)
	return
}

func (u *foodListServer) GetStock(openId string) (stock string, err error) {
	food := FoodList{}
	err = Db.QueryRow("SELECT stock FROM food_list WHERE open_id = ?", openId).Scan(&food.Stock)
	stock = food.Stock
	return
}

func (u *foodListServer) SetStock(openId string, stock string) (err error) {
	statement := "UPDATE food_list SET stock = ? WHERE open_id = ?"
	stmt, err := Db.Prepare(statement)
	if err != nil {
		return
	}
	defer stmt.Close()

	_, err = stmt.Exec(stock, openId)
	return
}

func (u *foodListServer) OpenStore(openId string) (err error) {
	statement := "UPDATE food_list SET opened = 1 WHERE open_id = ?"
	stmt, err := Db.Prepare(statement)
	if err != nil {
		return
	}
	defer stmt.Close()

	_, err = stmt.Exec(openId)
	return
}

func (u *foodListServer) StoreLevelUp(openId string, level int) (err error) {
	statement := "UPDATE food_list SET levels = ?, opened = 0 WHERE open_id = ?"
	stmt, err := Db.Prepare(statement)
	if err != nil {
		return
	}
	defer stmt.Close()

	_, err = stmt.Exec(level, openId)
	return
}

func (u *foodListServer) TaskLevelUp(openId string, taskLevel string) (err error) {
	statement := "UPDATE food_list SET task_level = ? WHERE open_id = ?"
	stmt, err := Db.Prepare(statement)
	if err != nil {
		return
	}
	defer stmt.Close()

	_, err = stmt.Exec(taskLevel, openId)
	return
}

func (u *foodListServer) TaskDataUp(openId string, taskData string) (err error) {
	statement := "UPDATE food_list SET task_data = ? WHERE open_id = ?"
	stmt, err := Db.Prepare(statement)
	if err != nil {
		return
	}
	defer stmt.Close()

	_, err = stmt.Exec(taskData, openId)
	return
}

func (u *foodListServer) UpCompletes(openId string, completes string) (err error) {
	statement := "UPDATE food_list SET completes = ? WHERE open_id = ?"
	stmt, err := Db.Prepare(statement)
	if err != nil {
		return
	}
	defer stmt.Close()

	_, err = stmt.Exec(completes, openId)
	return
}
